//
//  CPWordViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/8/3.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "CPWordViewController.h"
#import "Base.h"
#import "Loadsingle.h"
#import "UserModel.h"
#import "ReadyDiSignle.h"
@interface CPWordViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField *oldpField;
@property(nonatomic,strong)UITextField *newpField;
@property(nonatomic,strong)UITextField *againpField;
@property(nonatomic,strong)UIButton *button;
@end

@implementation CPWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBack];
    self.view.backgroundColor=[UIColor whiteColor];
    self.oldpField=[[UITextField alloc] initWithFrame:CGRectMake(5, 65, WIDTH-10, 40)];
    
    self.newpField=[[UITextField alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(self.oldpField.frame)+5, WIDTH-10, 40)];
    
    self.againpField=[[UITextField alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(self.newpField.frame)+5, WIDTH-10, 40)];
    
    self.button=[[UIButton alloc] initWithFrame:CGRectMake(WIDTH/2-30, CGRectGetMaxY(self.againpField.frame)+10, 60, 40)];
    [self.button setTitle:@"确定" forState:UIControlStateNormal];
    self.button.backgroundColor=[UIColor redColor];
    
    self.oldpField.layer.borderColor=[[UIColor grayColor] CGColor];
    self.oldpField.layer.borderWidth=1.0;
    
    self.newpField.layer.borderColor=[[UIColor grayColor] CGColor];
    self.newpField.layer.borderWidth=1.0;
    
    self.againpField.layer.borderColor=[[UIColor grayColor] CGColor];
    self.againpField.layer.borderWidth=1.0;
    
    self.oldpField.placeholder=@"原密码";
    self.newpField.placeholder=@"新密码";
    self.againpField.placeholder=@"再次输入";
    self.oldpField.delegate=self;
    self.newpField.delegate=self;
    self.againpField.delegate=self;
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.newpField];
    [self.view addSubview:self.oldpField];
    [self.view addSubview:self.againpField];
    [self.view addSubview:self.button];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(void)buttonAction:(UIButton *)sender
{
    NSString *documents=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
   NSString *file=[documents stringByAppendingString:@"/userp.plist"];
    
    NSDictionary *password=[NSDictionary dictionaryWithContentsOfFile:file];
        // NSLog(@"%@",password);
   [password valueForKey:@"password"];
  if(![self.oldpField.text isEqualToString:[password valueForKey:@"password"]])
      {
      UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"密码错误" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
      [a show];
      }
   else if (self.newpField.text.length<6)
        {
        UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"密码最少6位" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [a show];
        }
    else if (![self.newpField.text isEqualToString:self.againpField.text])
        {
        UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"两次输入不一样" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [a show];
       }
    else
        {
       UserModel *user= [Loadsingle single].userArray[0];
            //NSLog(@"%@",user._id);
        [ReadyDiSignle signle].oidString=user._id;
        [[ReadyDiSignle signle] changePassWord:self.oldpField.text for:self.newpField.text];
        }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
